package view;

import com.sun.deploy.security.SelectableSecurityManager;
import control.PassengerController;
import model.Passenger;
import org.w3c.dom.Text;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class LogIn implements ActionListener{
    public static JFrame jfra;
    public static JPanel jpan1, jpan2, jpan3, jpan4, jpan5, jpan6, jpan7, jpan8,jpan9,jpan10,jpan11,myPanel,jpanMid;
    public static JLabel company, MyTripLabel, LogInLabel, ServiceLabel, VIPLabel, ModifyLabel, ContactLabel,lab1, lab2, lab3, lab4, lab5, lab6, lab7,label;
    public static JButton btn3,btn4,btn2;
    public static ImageIcon background;
    public static String currentPlaneNum;
    public static String bookNumber;
    int usebooknum = 0;      //if the passenger use booknum to login or not
    JTextField text1 = new JTextField(16);
    JTextField text4 = new JTextField(16);
    JTextField text2 = new JTextField(16);
    JTextField text3 = new JTextField(16);

    public void createPage(){
        jfra = new JFrame();


//setting the background
        background = new ImageIcon("src/view/image/background.png"); //Create a background image
        label = new JLabel(background);  //Add a background image to the label
        label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());//Set the label to be as wide as the image
        myPanel = (JPanel) jfra.getContentPane();  //Set myPanel to the Content panel
        myPanel.setOpaque(false);  //Make my panel invisible
        myPanel.setLayout(new FlowLayout());  //Set my panel to a flow layout
        jfra.getLayeredPane().setLayout(null);//Leave the layout of the layered panel blank

//create bar
        jpan1 = new JPanel();
        company = new JLabel("          Check-In Kiosk                    ", JLabel.LEFT);
        Font fnt = new Font("Serief", Font.BOLD, 40);
        company.setFont(fnt);
        LogInLabel = new JLabel("LogIn          ", JLabel.RIGHT);
        LogInLabel.setBackground(Color.YELLOW);
        MyTripLabel = new JLabel("MyTrip          ", JLabel.RIGHT);
        ServiceLabel = new JLabel("Service         ", JLabel.RIGHT);
        VIPLabel = new JLabel("VIP         ", JLabel.RIGHT);
        ModifyLabel = new JLabel("Board         ", JLabel.RIGHT);
        ContactLabel = new JLabel("Contact           ", JLabel.RIGHT);
        company.setForeground(Color.blue);
        jpan1.add(company);
        jpan1.add(LogInLabel);
        jpan1.add(MyTripLabel);
        jpan1.add(ServiceLabel);
        jpan1.add(VIPLabel);
        jpan1.add(ModifyLabel);
        jpan1.add(ContactLabel);
        jpan1.setVisible(true);


//create login box
        jpan2 = new JPanel(new BorderLayout());
        jpan2.setPreferredSize(new Dimension(320,460));
//create choice box
        jpan3=new JPanel(new GridLayout(1, 2));
        jpan4 = new JPanel();
        jpan4.setBackground(Color.WHITE);
        jpan4.setPreferredSize(new Dimension(180,85));
        jpan5 = new JPanel();
        jpan5.setBackground(Color.WHITE);

        btn2=new JButton();
        btn3=new JButton();
        btn4=new JButton();
//        Set the content within each button
        lab1 = new JLabel("Log In with", JLabel.CENTER);
        JLabel lab11 = new JLabel("booking number", JLabel.CENTER);
        Font fnt1 = new Font("Serief", Font.BOLD, 16);// Setting the size and style of the font.
        lab1.setFont(fnt1);
        lab11.setFont(fnt1);
        lab11.setForeground(Color.BLACK);
        lab1.setForeground(Color.BLACK);
        btn3.setLayout(new GridLayout(2, 1));
        btn3.add(lab1);
        btn3.add(lab11);
        jpan4.add(btn3, BorderLayout.SOUTH);
        jpan4.setVisible(true);
        jpan3.add(jpan4);

        lab2 = new JLabel("Log In with", JLabel.CENTER);
        JLabel lab22 = new JLabel("ID number", JLabel.CENTER);
        Font fnt2 = new Font("Serief", Font.BOLD, 16);// Setting the size and style of the font.
        lab2.setFont(fnt2);
        lab22.setFont(fnt2);
        lab2.setForeground(Color.BLACK);
        lab22.setForeground(Color.BLACK);
        btn4.setLayout(new GridLayout(2, 1));
        btn4.add(lab2);
        btn4.add(lab22);
        btn3.addActionListener(this);//Calling the listener
        btn4.addActionListener(this);//Calling the listener
        jpan5.add(btn4, BorderLayout.SOUTH);
        jpan5.setVisible(true);//Making the fifth panel visible
        jpan3.add(jpan5);
        jpan2.add(jpan3,BorderLayout.NORTH);
        jpan3.setVisible(true);
        jpan2.setVisible(true);

//set middle panel
        jpanMid=new JPanel();
        jpanMid.setLayout(new GridLayout(2, 1));
        jpan2.add(jpanMid,BorderLayout.CENTER);

//In the first case, only the booking number is contained in the middle box
//The booking number display box
        jpan6 = new JPanel();
        jpan6.setBackground(Color.WHITE);
        lab3=new JLabel("         Booking Number         ");
        Font fnt3 = new Font("Serief", Font.ITALIC, 20);// Setting the size and style of the font.
        lab3.setFont(fnt3);
        lab3.setForeground(Color.BLACK);
        jpan6.add(lab3,BorderLayout.SOUTH);
        jpanMid.add(jpan6);
        jpan6.setVisible(true);

//Booking number input box
        jpan11=new JPanel();
        jpan11.setBackground(Color.WHITE);
        text4 = new JTextField(16);
        Font fnt5 = new Font("Serief", Font.ITALIC, 18);
        text4.setFont(fnt5);
        text4.setHorizontalAlignment(JTextField.CENTER);//Setting this text box to center horizontally
        String info1 = "Booking Number";
        text4.setText(info1);
        text4.addFocusListener(new MyFocusListener(info1, text4));//添加焦点事件反映
        jpan11.add(text4);
        jpan11.setVisible(true);
        jpanMid.add(jpan11);
        jpanMid.setVisible(true);
        jpan2.add(jpanMid);

//Set the bottom panel
        JPanel jpanBot=new JPanel();
        jpanBot.setLayout(new GridLayout(3, 1));
        jpanBot.setBackground(Color.WHITE);

//set the SCAN ID CARD label
        jpan7=new JPanel();
        jpan7.setBackground(Color.WHITE);
        lab4=new JLabel("Scan ID Card");
        lab4.setForeground(Color.blue);
        jpan7.add(lab4);
        jpan7.setVisible(true);
        jpanBot.add(jpan7);
        jpanBot.setVisible(true);

//Set the Login button part
        jpan8=new JPanel();
        jpan8.setBackground(Color.WHITE);
        lab5=new JLabel("LOG IN");
        fnt3 = new Font("Serief", Font.BOLD, 26);// Setting the size and style of the font.
        lab5.setFont(fnt3);
        btn2.add(lab5);
        btn2.setVisible(true);
        btn2.addActionListener(this);//Calling the listener
        jpan8.add(btn2);
        jpan8.setVisible(true);
        jpanBot.add(jpan8);
        jpanBot.setVisible(true);
        jpan2.add(jpanBot,BorderLayout.SOUTH);
        jpan2.setVisible(true);

//Set myPanel
        myPanel.add(jpan1);
        myPanel.add(jpan2,BorderLayout.CENTER);
        jfra.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
//Set interface properties
        jfra.setBounds(300, 300, background.getIconWidth(), background.getIconHeight());
        jfra.setVisible(true);
        jfra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e the event to be processed
     */

    @Override
    public void actionPerformed(ActionEvent e) {
        boolean choose1=(e.getSource()==btn3);
        boolean choose2=(e.getSource()==btn4);
        boolean login=(e.getSource()==btn2);


        if(choose2){
//remove the original panel
            usebooknum = 1;
            jpanMid.removeAll();
            jpanMid.repaint();
//set surname part
            jpan9 = new JPanel();
            jpan9.setBackground(Color.WHITE);
            lab6=new JLabel("Surname   ");
            Font fnt4 = new Font("Serief", Font.ITALIC, 15);// Setting the size and style of the font.
            lab6.setFont(fnt4);
            lab6.setForeground(Color.BLACK);
            jpan9.add(lab6);
            text2.setHorizontalAlignment(JTextField.CENTER);
            String info2 = "Surname";
            text2.setText(info2);
            text2.addFocusListener(new MyFocusListener(info2, text2));
            jpan9.add(text2);
            jpan9.setVisible(true);
            jpanMid.add(jpan9);
            jpanMid.setVisible(true);
            jpanMid.revalidate();
//set ID number padding
            jpan10 = new JPanel();
            jpan10.setBackground(Color.WHITE);
            lab7=new JLabel("ID Number  ");
            Font fnt5 = new Font("Serief", Font.ITALIC, 15);// Setting the size and style of the font.
            lab7.setFont(fnt5);
            lab7.setForeground(Color.BLACK);
            jpan10.add(lab7);

            text3.setHorizontalAlignment(JTextField.CENTER);//Setting this text box to center horizontally
            String info3 = "ID Number";
            text3.setText(info3);
            text3.addFocusListener(new MyFocusListener(info3, text3));//Adds a focus event reflection
            jpan10.add(text3);
            jpan10.setVisible(true);
            jpanMid.add(jpan10);
            jpanMid.setVisible(true);
            jpanMid.revalidate();
//reset the panel

        }else if(choose1){
//Change the button options again
            usebooknum = 2;
            jpanMid.removeAll();
            jpanMid.repaint();

            jpan6 = new JPanel();
            jpan6.setBackground(Color.WHITE);
            lab3=new JLabel("         Booking Number         ");
            Font fnt3 = new Font("Serief", Font.ITALIC, 20);// Setting the size and style of the font.
            lab3.setFont(fnt3);
            lab3.setForeground(Color.BLACK);
            jpan6.add(lab3,BorderLayout.SOUTH);
            jpanMid.add(jpan6);
            jpan6.setVisible(true);

            jpan11=new JPanel();
            jpan11.setBackground(Color.WHITE);

            Font fnt5 = new Font("Serief", Font.ITALIC, 18);
            text1.setFont(fnt5);
            text1.setHorizontalAlignment(JTextField.CENTER);//Setting this text box to center horizontally
            String info1 = "Booking Number";
            text1.setText(info1);
            text1.addFocusListener(new MyFocusListener(info1, text1));//添加焦点事件反映
            jpan11.add(text1);
            jpan11.setVisible(true);
            jpanMid.add(jpan11);
            jpanMid.setVisible(true);
            jpanMid.revalidate();
        }else if(login){                                                  // if the user click the "login" button
            System.out.println(usebooknum);
            String str1=text1.getText().toString();      //back booknum
            String str2=text2.getText().toString();       // surname
            String str3=text3.getText().toString();       // id
            String str4=text4.getText().toString();        // ori booknum
            boolean b1 = (str1==null||str1.equals(""));                   // examine the input
            boolean b2 = (str2==null||str2.equals(""));
            boolean b3 = (str3==null||str3.equals(""));
            boolean b4 = (str2.equals("Surname"));
            boolean b5 = (str3.equals("ID Number"));
            boolean b6 = (str1.equals("Booking Number"));
            boolean b7 = (str4==null||str4.equals(""));
            boolean b8 = (str4.equals("Booking Number"));

//            System.out.println(str1);
//            System.out.println(str2);
//            System.out.println(str3);
//            System.out.println(str4);
//            System.out.println("b5"+b5);
//            System.out.println("b2"+b2);
//            System.out.println("b3"+b3);
//            System.out.println("b4"+b4);

            if(usebooknum==0) {
                if (b7 != true && b8 != true)         // search in database to verify for the passenger

                {
                    PassengerController pc = new PassengerController();
                    if(pc.checkBookNumber(str4)==true){
                    Passenger p = pc.queryByBookNumber(str4);
                    System.out.println("pid" + p.getID());
                    if (p.getID() == null) {

                        JOptionPane.showMessageDialog(null, "No such booking number", "Error", JOptionPane.ERROR_MESSAGE);
                    } else {
                        bookNumber = str4;
                        System.out.println("bookNumber = " + bookNumber);
                        //new www();
                        jfra.setVisible(false);
                        view.mytrips tips = new view.mytrips();
                    }
                }
                    else JOptionPane.showMessageDialog(null, "Please check format!", "Error", JOptionPane.ERROR_MESSAGE);
                }
                else JOptionPane.showMessageDialog(null, "Please input booking number", "Error", JOptionPane.ERROR_MESSAGE);}
            else if(usebooknum==1) {
                if (b2 != true && b3 != true && b4 != true && b5 != true) {
                    PassengerController pc = new PassengerController();
                    if (pc.checkIDNumber(str3) == true) {
                        Passenger p = pc.queryBySurnameAndID(str2, str3);
                        System.out.println(str2);
                        System.out.println(str3);
                        System.out.println("pid" + p.getID());
                        if (p.getID() == null)
                            JOptionPane.showMessageDialog(null, "No such passenger", "Error", JOptionPane.ERROR_MESSAGE);
                        else {
                            bookNumber = p.getBookNumber();
                            System.out.println("bookNumber = " + bookNumber);
                            //new www();
                            jfra.setVisible(false);
                            view.mytrips tips = new view.mytrips();
                        }
                    }else  JOptionPane.showMessageDialog(null, "Please check your format", "Error", JOptionPane.ERROR_MESSAGE);

                } else
                        JOptionPane.showMessageDialog(null, "Please input your personal information", "Error", JOptionPane.ERROR_MESSAGE);

            }
            else if(usebooknum==2)
            {
                if ( b1 != true && b6 != true) {
                PassengerController pc = new PassengerController();
                    if(pc.checkBookNumber(str1)==true){
                Passenger p = pc.queryByBookNumber(str1);
                System.out.println("pid" + p.getID());
                if (p.getID() == null) {
                    JOptionPane.showMessageDialog(null, "No such booking number", "Error", JOptionPane.ERROR_MESSAGE);
                } else {
                    bookNumber = str1;
                    System.out.println("bookNumber = " + bookNumber);
                    //new www();
                    jfra.setVisible(false);
                    view.mytrips tips = new view.mytrips();
                }
            } else JOptionPane.showMessageDialog(null, "Please check format!", "Error", JOptionPane.ERROR_MESSAGE);}
            else
                JOptionPane.showMessageDialog(null, "Please input your booking number!", "Error", JOptionPane.ERROR_MESSAGE);}






        }

    }

//Text boxes display text by default
    class MyFocusListener implements FocusListener {
        String info;
        JTextField jtf;

        public MyFocusListener(String info, JTextField jtf) {
            this.info = info;
            this.jtf = jtf;
            jtf.setText(info);//The default is direct display
            jtf.setForeground(Color.GRAY);
        }
//When you get the focus, clear the prompt text
        @Override
        public void focusGained(FocusEvent e) {
            String temp = jtf.getText();
            if (temp.equals(info)) {
                jtf.setText("");
                jtf.setForeground(Color.BLACK);
            }
        }
//When the focus is lost, if the judgment is empty, the prompt text is displayed
        @Override
        public void focusLost(FocusEvent e) {
            String temp = jtf.getText();
            if (temp.equals("")) {
                jtf.setForeground(Color.GRAY);
                jtf.setText(info);
            }
        }
    }


}